﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using BlueMarineWindowsFormsClient.ManagerServiceReference;

namespace BlueMarineWindowsFormsClient
{
    public partial class frmHotelManager : Form
    {
        private const string Error = "ERROR: ";
        private readonly int _eqheight;
        private readonly int _height;
        private readonly ManagerServiceClient _roomManagement;
        private readonly int _rtheight;
        private readonly int p2h = 120;
        private readonly int p3h = 109;

        public frmHotelManager()
        {
            InitializeComponent();
            _height = Height;
            _rtheight = dtgRoomType.Height;
            _eqheight = dtgRoTyEq.Height;
            Temp_EquipmentDetails = new List<EquipmentDetail>();
            _roomManagement = new ManagerServiceClient("nettcp1");
            MinimumSize = new Size(Width,Height);
        }

        public override sealed Size MinimumSize
        {
            get { return base.MinimumSize; }
            set { base.MinimumSize = value; }
        }

        public List<EquipmentDetail> Temp_EquipmentDetails { get; set; }

        private void tabmain_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (hotelManager.SelectedIndex)
            {
                case 0:
                    ViewEquipment();
                    break;
                case 1:
                    ViewRoomType();
                    break;
                case 2:
                    ViewRoom();
                    FillCbxRoomType();
                    break;
                case 3:
                    ViewSupply();
                    break;
            }
        }

        //tab1

        private void RoomManagement_Load(object sender, EventArgs e)
        {
            ViewEquipment();
        }

        private void dtgEquipment_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgEquipment.CurrentRow != null)
                {
                    int id = int.Parse(dtgEquipment.Rows[dtgEquipment.CurrentRow.Index].Cells[0].Value.ToString());
                    EquipmentDetail equipmentDetail = _roomManagement.EquGetByID(id);
                    if (equipmentDetail == null) return;
                    txtEqID.Text = equipmentDetail.EquipmentDetailID.ToString(CultureInfo.InvariantCulture);
                    txtEqName.Text = equipmentDetail.EquipmentName;
                    txtEqPrice.Text = equipmentDetail.EquipmentPrice.ToString(CultureInfo.InvariantCulture);
                    chbxAvailableEquipment.Checked = equipmentDetail.Available;
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnAddEq_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Equipment...";
                string result = _roomManagement.EquAdd(txtEqName.Text, decimal.Parse(txtEqPrice.Text),
                                                       chbxAvailableEquipment.Checked);

                lblStatus.Text = result == "SUCCESS" ? "SUCCESS: Create Equipment" : result;
                ViewEquipment();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void chbxViewAvaiEq_CheckedChanged(object sender, EventArgs e)
        {
            ViewEquipment();
        }

        private void btnUpdateEq_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Equipment...";
                string result = _roomManagement.EquModify(int.Parse(txtEqID.Text), txtEqName.Text,
                                                          decimal.Parse(txtEqPrice.Text),
                                                          chbxAvailableEquipment.Checked);
                lblStatus.Text = result == "SUCCESS" ? "SUCCESS: Update Equipment" : result;
                ViewEquipment();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void ViewEquipment()
        {
            try
            {
                List<EquipmentDetail> data = _roomManagement.EquGet(chbxViewAvaiEq.Checked);
                var source = new BindingSource {DataSource = data};
                dtgEquipment.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        //tab2

        private void dtgRoomType_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgRoomType.CurrentRow != null)
                {
                    int id = int.Parse(dtgRoomType.Rows[dtgRoomType.CurrentRow.Index].Cells[0].Value.ToString());
                    Temp_EquipmentDetails = _roomManagement.EquGetByRoomTypeID(id).ToList();
                    RoomType roomType = _roomManagement.RoTyGetByID(id);
                    txtRoTyID.Text = roomType.RoomTypeID.ToString(CultureInfo.InvariantCulture);
                    txtRoTyTitle.Text = roomType.RoomTypeTitle;
                    txtRoTyRank.Text = roomType.RoomRank;
                    txtRoTyPrice.Text = roomType.RoomPrice.ToString(CultureInfo.InvariantCulture);
                    chbxBalcony.Checked = roomType.Balcony;
                    chbxBath.Checked = roomType.Bath;
                    chbxView.Checked = roomType.View;
                }

                viewRoomTypeEquipment();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnAddRoTyEq_Click(object sender, EventArgs e)
        {
            var equipmentSelect = new frmEquipmentSelect(this);
            equipmentSelect.Show();
        }

        private void btnRemoveRoTyEq_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridViewSelectedRowCollection idxs = dtgRoTyEq.SelectedRows;
                for (int i = 0; i < idxs.Count; i++)
                {
                    int idx = idxs[i].Index;
                    Temp_EquipmentDetails.RemoveAt(idx);
                }
                viewRoomTypeEquipment();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnAddRoomType_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Room Type...";
                var ids = new List<int>();
                for (int i = 0; i < dtgRoTyEq.Rows.Count; i++)
                {
                    ids.Add(int.Parse(dtgRoTyEq.Rows[i].Cells[0].Value.ToString()));
                }
                string result = _roomManagement.RoTyAdd(txtRoTyTitle.Text, txtRoTyRank.Text,
                                                        decimal.Parse(txtRoTyPrice.Text),
                                                        chbxBath.Checked,
                                                        chbxView.Checked, chbxBalcony.Checked, ids);
                lblStatus.Text = result == "SUCCESS" ? "SUCCESS: Create Room Type" : result;
                ViewRoomType();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnUpdateRoomType_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Room Type...";
                var ids = new List<int>();
                for (int i = 0; i < dtgRoTyEq.Rows.Count; i++)
                {
                    ids.Add(int.Parse(dtgRoTyEq.Rows[i].Cells[0].Value.ToString()));
                }
                string result = _roomManagement.RoTyModify(int.Parse(txtRoTyID.Text), txtRoTyTitle.Text,
                                                           txtRoTyRank.Text,
                                                           decimal.Parse(txtRoTyPrice.Text),
                                                           chbxBath.Checked, chbxView.Checked, chbxBalcony.Checked, ids);

                lblStatus.Text = result == "SUCCESS" ? "SUCCESS: Update Room Type" : result;
                ViewRoomType();
                viewRoomTypeEquipment();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        public void viewRoomTypeEquipment()
        {
            var source = new BindingSource {DataSource = Temp_EquipmentDetails};
            dtgRoTyEq.DataSource = source;
        }

        private void ViewRoomType()
        {
                List<RoomType> data = _roomManagement.RoTyGet();
                var source = new BindingSource {DataSource = data};
                dtgRoomType.DataSource = source;
        }

        //tab3

        private void FillCbxRoomType()
        {
            try
            {
                cbxRoomType.Items.Clear();
                List<RoomType> data = _roomManagement.RoTyGet();
                foreach (RoomType roomType in data)
                {
                    cbxRoomType.Items.Add(roomType);
                }
                cbxRoomType.DisplayMember = "RoomTypeTitle";
                cbxRoomType.ValueMember = "RoomTypeID";
                if (data.Count > 0)
                {
                    cbxRoomType.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void dtgRoom_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgRoom.CurrentRow != null)
                {
                    int id = int.Parse(dtgRoom.Rows[dtgRoom.CurrentRow.Index].Cells[0].Value.ToString());
                    Room room = _roomManagement.RooGetByID(id);
                    txtRoomId.Text = room.RoomID.ToString(CultureInfo.InvariantCulture);
                    switch (room.Status)
                    {
                        case 0:
                            radRoomOccupied.Checked = true;
                            break;
                        case 1:
                            radRoomAvailable.Checked = true;
                            break;
                        default:
                            radRoomUnavailable.Checked = true;
                            break;
                    }
                    for (int i = 0; i < cbxRoomType.Items.Count; i++)
                    {
                        var roomType = (RoomType) cbxRoomType.Items[i];
                        if (roomType.RoomTypeID == room.RoomTypeID)
                        {
                            cbxRoomType.SelectedIndex = i;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnAddRoom_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Room...";
                var roomType = (RoomType) cbxRoomType.SelectedItem;
                string roomTypeId = roomType.RoomTypeID.ToString(CultureInfo.InvariantCulture);
                string state = "-1";
                if (!radRoomUnavailable.Checked)
                {
                    state = !radRoomOccupied.Checked ? "1" : "0";
                }
                string result = _roomManagement.RooAdd(int.Parse(txtRoomId.Text), int.Parse(state),
                                                       int.Parse(roomTypeId));
                lblStatus.Text = result;
                lblStatus.Text = result == "SUCCESS" ? "SUCCESS: Create Room" : result;
                ViewRoom();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnUpdateRoom_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Room...";
                var roomType = (RoomType) cbxRoomType.SelectedItem;
                string roomTypeId = roomType.RoomTypeID.ToString(CultureInfo.InvariantCulture);
                string state = "-1";
                if (!radRoomUnavailable.Checked)
                {
                    state = !radRoomOccupied.Checked ? "1" : "0";
                }
                string result = _roomManagement.RooModify(int.Parse(txtRoomId.Text), int.Parse(state),
                                                          int.Parse(roomTypeId));
                lblStatus.Text = result == "SUCCESS" ? "SUCCESS: Update Room" : result;
                ViewRoom();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void frmRoomManager_Resize(object sender, EventArgs e)
        {
            double x1 = (Height - _height)*0.4;
            double x2 = (Height - _height)*0.6;
            dtgRoTyEq.Height = _eqheight + (int) x1;
            dtgRoomType.Height = _rtheight + (int) x2;

            var resH = Height - _height;
            resH = (resH - 6)/2;
            panel2.Height = p2h + resH;
            panel3.Height = p3h+ resH;

        }

        private void btnSearchStatus_Click(object sender, EventArgs e)
        {
            ViewRoom();
        }

        private void btnSearchEquipment_Click(object sender, EventArgs e)
        {
            try
            {
                List<EquipmentDetail> data = _roomManagement.EquGetByName(txtEqName.Text);
                var source = new BindingSource {DataSource = data};
                dtgEquipment.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;
            }
        }

        private void btnSeachByRoomTypeTitle_Click(object sender, EventArgs e)
        {
            try
            {
                var roomType = (RoomType) cbxRoomType.SelectedItem;
                int id = roomType.RoomTypeID;
                List<Room> data = _roomManagement.RooGetByRoTyID(id);
                var source = new BindingSource {DataSource = data};
                dtgRoom.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnSeachRoom_Click(object sender, EventArgs e)
        {
            try
            {
                Room room = _roomManagement.RooGetByID(int.Parse(txtRoomId.Text));
                if (room == null)
                {
                    lblStatus.Text = string.Format("No room name {0}", txtRoomId.Text);
                    return;
                }
                txtRoomId.Text = room.RoomID.ToString(CultureInfo.InvariantCulture);
                switch (room.Status)
                {
                    case 0:
                        radRoomOccupied.Checked = true;
                        break;
                    case 1:
                        radRoomAvailable.Checked = true;
                        break;
                    default:
                        radRoomUnavailable.Checked = true;
                        break;
                }
                for (int i = 0; i < cbxRoomType.Items.Count; i++)
                {
                    var roomType = (RoomType) cbxRoomType.Items[i];
                    if (roomType.RoomTypeID == room.RoomTypeID)
                    {
                        cbxRoomType.SelectedIndex = i;
                    }
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void ViewRoom()
        {
            try
            {
                int stt = 1;
                if (radRoomOccupied.Checked)
                {
                    stt = 0;
                }
                else if (radRoomUnavailable.Checked)
                {
                    stt = -1;
                }
                List<Room> data = _roomManagement.RooGet(stt);
                var source = new BindingSource {DataSource = data};
                dtgRoom.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        //Tab4

        private void btnAddSu_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Supply Detail...";
                string result = _roomManagement.SupDetailAdd(chbxAvailableSupply.Checked, txtSupplyName.Text,
                                                             decimal.Parse(txtSupplyPrice.Text), txtSupplyUnit.Text);
                lblStatus.Text = result == "SUCCESS" ? "SUCCESS: Create Supply Detail" : result;
                ViewSupply();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnSearchSupply_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Searching Supply Detail...";
                List<SupplyDetail> data = _roomManagement.SupDetailGetByName(txtSupplyName.Text);
                var source = new BindingSource {DataSource = data};
                dtgSupply.DataSource = source;
                lblStatus.Text = "SUCCESS: Search Supply Detail...";
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnUpdateSu_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Supply Detail...";
                string result = _roomManagement.SupDetailModify(int.Parse(txtSupplyId.Text), chbxAvailableSupply.Checked,
                                                                txtSupplyName.Text, decimal.Parse(txtSupplyPrice.Text),
                                                                txtSupplyUnit.Text);
                lblStatus.Text = result == "SUCCESS" ? "SUCCESS: Create Supply Detail" : result;
                ViewSupply();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void chbxViewAvaiSu_CheckedChanged(object sender, EventArgs e)
        {
            ViewSupply();
        }

        private void dtgSupply_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgSupply.CurrentRow != null)
                {
                    var supplyDetail =
                        _roomManagement.SupDetailGetById(
                            int.Parse(dtgSupply.Rows[dtgSupply.CurrentRow.Index].Cells[0].Value.ToString()));
                    txtSupplyName.Text = supplyDetail.SupplyName;
                    txtSupplyId.Text = supplyDetail.SupplyDetailID.ToString();
                    txtSupplyPrice.Text = supplyDetail.SupplyPrice.ToString();
                    txtSupplyUnit.Text = supplyDetail.Unit;
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void ViewSupply()
        {
            try
            {
                List<SupplyDetail> data = _roomManagement.SupDetailGet(chbxViewAvaiSu.Checked);
                var source = new BindingSource {DataSource = data};
                dtgSupply.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void tabPromotion_Click(object sender, EventArgs e)
        {

        }

      
    }
}